package com.zpp.common.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zpp.common.core.constants.Constants;
import com.zpp.common.core.utils.ThreadLocalUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //插入时自动填充字段
    @Override
    public void insertFill(MetaObject metaObject) {
        Long createId = ThreadLocalUtil.get(Constants.USER_ID,Long.class) == null ? 1L : ThreadLocalUtil.get(Constants.USER_ID,Long.class);
        this.strictInsertFill(metaObject, "createId", Long.class, createId);
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
    }

    //更新时自动填充字段
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateId", Long.class, ThreadLocalUtil.get(Constants.USER_ID,Long.class));
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
